<?php
ob_start();
session_start();

?>
<script type="text/javascript"> //Initialize_JoptionPane(); </script>
<?php

function message_convert_url($string){
  return preg_replace("~(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)~", "<a href=\"$1://$2\"  target=\"_blank\" rel=\"nofollow\">$1://$2</a>$3", $string);
}


$id=$_SESSION['id'];
$name=$_SESSION['name'];
$logo=$_SESSION['logo'];
$flag=0;

if(!isset($_POST['offset'])){ 
    
    $startoffset=0; $endoffset=16;
    
}else{

$startoffset=$_POST['offset']-17; 

$endoffset= $_POST['offset'];

//echo$startoffset.$endoffset;break;


}
   


if (isset($_GET['pag'])){
   $pag = $_GET['pag'];  
   $_SESSION['current_page']=$pag;
 
}else{
   if(isset($_SESSION['current_page'])){
       $pag=$_SESSION['current_page'];
   }else{  
       $pag=1;
   }
} 

  try{      
      
     include_once('restconnection.php'); 
     if(isset($client)){  
      
         if(isset($_POST['where_db'])){ 
             $where_db=$_POST['where_db'];
             $where=  str_replace("1","'",$where_db);
            
         }else{
             $where="";
           
         }
         
   //print_r($where); break;
        //chiamata query home_timeline facebook
        
     
        //retrive base url 
        $url = $_SESSION['basic_url'];
        //add service url
        $service_url="/GetMyPosts?";
        //params url
        $params = array('id'=>$id,'where'=>$where,'start_offset'=>$startoffset,'end_offset'=>$endoffset); 
        //params connection
        $ctx = stream_context_create(array('http' => array('timeout' => 30))); 
        //send GET request
        $final_url = $url.$service_url."id=".$params['id']."&"."where=".urlencode($params['where'])."&"."start_offset=".$params['start_offset']."&"."end_offset=".$params['end_offset'];
        //print_r($final_url); break;
        $output = @file_get_contents($final_url,0,$ctx);
        //parse JSON output
        //print_r($url.$service_url."id=".$params['id']."&"."where=".$params['where']."&"."start_offset=".$params['start_offset']."&"."end_offset=".$params['end_offset']);
        $data = json_decode($output);
        //var_dump($output); break;
        $webResult = $data->GetMyPostsResult;
       // print_r($webResult); break;
        

        //split and riorganize Facebook message_post  
        if($webResult[0]!= null){   
        
             $get_posts=$webResult;
              //number of all records devono essere 11
             $num_records=count($get_posts);
            // echo$num_records; 
            // break;
            
             
             if($num_records!=1){
                for($i=0;$i<=15;$i++){
                   if($get_posts[$i]!=null){
                       
                        $split = explode("+-+",$get_posts[$i]);
                        
                        $mess[$i]=$split[0];   
                        $message[$i]=message_convert_url($mess[$i]);
                        
                        $datetime[$i]=$split[1];
                        $datetime[$i]=substr($datetime[$i], 0, -3);
                        $datetime[$i]=str_replace(".",":",$datetime[$i]);
                        
                        $socialnet[$i]=$split[2];
                        $img_message[$i]=$split[3];
                        $num_likes[$i]=$split[4];
                        $num_comments[$i]=$split[5];
                        $num_shares[$i]=$split[6];
                        
                        
                        
                   }
  
                }//for
                
                if($get_posts[15]!=null){
                     
                    $flag=1;
                    //significa che il 15° elemento è pieno e quindi mostro il bottone
                    
                }//if
               
               
             
             }else{
                
                    $split = explode("+-+",$get_posts);
  
                    $message[0]=$split[0];
                    $message[0]=message_convert_url($message[0]);
                    $datetime[0]=$split[1];
                    $datetime[0]=substr($datetime[$i], 0, -3);
                    $datetime[0]=str_replace(".",":",$datetime[0]);
                    $socialnet[0]=$split[2];
                    $img_message[0]=$split[3];
                    $num_likes[0]=$split[4];
                    $num_comments[0]=$split[5];
                    $num_shares[0]=$split[6];
              }    

                ?>
               
                <div  class="timeline" id="timeline">
                <?php
                for($i=0;$i<15;$i++){
                
                    if($get_posts[$i]!=null){
        ?>             
                        <div  class="user_post" id="post<?php echo$i;?>">
                        <div id="avatar_writer" class="user_avatar fl" ><img src="<?php echo$logo; ?>" title="<?php echo$name; ?>" /> </div> 
                        <div class="other fr">
                            <?php 
                            if($socialnet[$i] == "Facebook") echo"<img class='icon_post_social' src='esa/img/img_user_provenience_posts/fb.png' title='$socialnet[$i]' />";
                            if($socialnet[$i] == "Google")  echo"<img class='icon_post_social'   src='esa/img/img_user_provenience_posts/go.png' title='$socialnet[$i]' />";
                            if($socialnet[$i] == "Twitter") echo"<img class='icon_post_social'  src='esa/img/img_user_provenience_posts/tw.png' title='$socialnet[$i]' />";
                            if($socialnet[$i] == "Pinterest")  echo"";
                            if($socialnet[$i] == "Linkedin")  echo"";
                            if($socialnet[$i] == "Foursquare") echo"";  
                            
                            ?> 
                           
                            <div id="user_name" class="user_name fl"><?php echo$name; ?></div><div class="clear"></div>
                            <div id="user_message_date" class="user_message_date fl" ><?php echo" il $datetime[$i] "; ?></div>
                        </div>    
                        <div class="clear"></div><br />
                        <div id="user_message" class="user_message"><?php echo" $message[$i] ";  ?></div>
                        <?php if($img_message[$i]!=""){ ?>
                        <img src="<?php echo" $img_message[$i] "; ?>" />
                        <?php } ?>
                        <div class="information_post">
                            <?php
                        
                            if($socialnet[$i] == "Facebook") echo"<div id='stats' ><img style='width:15px;' class='fl' alt='likes' title='likes' src='esa/img/likes.jpg' /><label class='fl'>".$num_likes[$i]."</label> <img style='width:25px;' class='fl'  alt='comments' title='comments' src='esa/img/comments.png' /><label class='fl'>".$num_comments[$i]."</label><div class='clear'></div></div>";
                            if($socialnet[$i] == "Google")   echo"<div id='stats'  ><label style='width:15px; margin-right:3px;' class='fl' alt='likes' title='likes'>  +".$num_likes[$i]."  </label> <img class='fl'  style='width:25px;padding:0px 0px 0px 7px;'  alt='comments' title='comments' src='esa/img/comments.png' /><label class='fl'>".$num_comments[$i]."</label><img style='width:40px; float:left'  alt='shares' title='shares' src='esa/img/share_g+.jpg' /><label class='fl'>".$num_shares[$i]."</label> <div class='clear'></div> </div>";
                            if($socialnet[$i] == "Twitter")  echo"<div id='stats'  ><img style='width:55px;' class='fl'  alt='retweets' title='retweets' src='esa/img/tweet.png' /><label class='fl'>".$num_shares[$i]."</label> <div class='clear'></div> </div>";
                            
                            ?>
                       <div class="clear"></div>     
                       </div>    
                        <div class="clear"></div>
                   </div>    
        <?php
                   }//if
                }//for
                              
            ?>
                   
                </div>
                
            <?php    
          }else{
              ?>
              <label style="margin-left:0px;">There are no posts </label>
             <!-- <script type='text/javascript'>ReduceDiv(".scrolltimelines");</script> -->
              <?php  
          }
            
     }//isset(client)    
     //echo$flag;
     if($flag==1){ $flag=0; ?>
              
         <script> can_download_other_posts('home')</script>
         <div id="otherposts" >
              <label><a title="Load Other Post" >Other Posts</a></label><br />             
              <img id="imgloading" class="hidden"  src="esa/img/loading.gif" />    
              <input type="hidden" value="2" id="numberclick" />
              
        </div> 
        
          
    <?php }
            
  }catch (Exception $e){
       
      ?>
          <label style="color:red;font-size:15px;text-align:center">Sorry General Error!</label><br/>
          <label><a><element onclick=" var link='esa/home.php'; ovv_mainother(link);" > Click to continue </a></label>
          
      <?php    
       
   }        
        ?>    
  